<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
    <el-form label-position="right" :model="parkingChargeForm" :label-width="formLabelWidth" ref="refParkingChargeForm" :rules="rules">
      <el-row>
        <el-col :span="11">
          <el-form-item label="车位号" prop="parkingAreaNumber">
            <el-input v-model="parkingChargeForm.parkingAreaNumber" placeholder="请选择车位号" readonly="readonly">
              <i slot="suffix" class="el-input__icon el-icon-search" @click="findParkingArea"></i>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="单元房号" prop="residentRoomNumber">
            <el-input v-model="parkingChargeForm.residentRoomNumber" placeholder="请选择单元房号" readonly="readonly">
              <i slot="suffix" class="el-input__icon el-icon-search" @click="findRoom"></i>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="车牌" prop="licensePlate">
            <el-input v-model="parkingChargeForm.licensePlate" placeholder="请输入车牌" maxlength="32"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="使用开始时间" prop="billingStartDate">
            <el-date-picker
                v-model="parkingChargeForm.billingStartDate" type="date" placeholder="使用开始时间" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="使用结束时间" prop="billingEndDate">
            <el-date-picker
                v-model="parkingChargeForm.billingEndDate" type="date" placeholder="使用结束时间" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="金额" prop="amount">
            <el-input-number v-model="parkingChargeForm.amount" :min="1" :max="1"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="parkingChargeForm.remark" placeholder="请输入备注" maxlength="255"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onSave">保存</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog :visible.sync="parkingAreaSelectorVisible" v-if="parkingAreaSelectorVisible" title="车位信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <parking-area-selector @parking-area-selected-event="handleParkingAreaSelectedEvent">
      </parking-area-selector>
    </el-dialog>
    <el-dialog :visible.sync="roomSelectorVisible" v-if="roomSelectorVisible" title="单元信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <room-selector @room-selected-event="handleRoomSelectedEvent">
      </room-selector>
    </el-dialog>
  </div>
</template>

<script>
import parkingChargeApi from '@/api/parkingChargeApi'
import ParkingAreaSelector from "@/views/parkingArea/ParkingAreaSelector.vue";
import RoomSelector from "@/views/room/RoomSelector.vue";
export default {
  name: 'ParkingChargeAdd',
  components: {ParkingAreaSelector,RoomSelector,},
  props: ['id'],
  mixins: [
  ],
  data () {
    return {
      parkingChargeForm: {
        parkingAreaId: '',
        parkingAreaNumber: '',
        residentRoomId: '',
        residentRoomNumber: '',
        licensePlate: '',
        billingStartDate: '',
        billingEndDate: '',
        amount: '',
        remark: ''
      },
      rules: {
        parkingAreaNumber: [
          { required: true, message: '请输入车位号', trigger: 'blur' }
        ],
        residentRoomNumber: [
          { required: true, message: '请输入单元房号', trigger: 'blur' }
        ],
      },
      parkingAreaSelectorVisible: false,
      roomSelectorVisible: false,
    }
  },
  created () {
    if (this.id) {
      parkingChargeApi.findById(Number(this.id))
        .then(resp => {
          if (resp && resp.code === 1) {
            this.parkingChargeForm = resp.data
          } else {
            this.$message.error('获取数据失败')
          }
        })
        .catch(error => {
          console.error('获取数据失败:', error)
        })
    }
  },
  methods: {
    onReset () {
      this.parkingChargeForm = {
        parkingAreaId: '',
        parkingAreaNumber: '',
        residentRoomId: '',
        residentRoomNumber: '',
        licensePlate: '',
        billingStartDate: '',
        billingEndDate: '',
        amount: '',
        remark: ''
      }
      this.$emit('reset-parking-charge-add-event')
    },
    onSave () {
      this.$refs.refParkingChargeForm.validate((valid) => {
        if (valid) {
          parkingChargeApi.save(this.parkingChargeForm)
            .then((resp) => {
              if (resp && resp.code === 1) {
                this.$message.success('保存成功')
                this.$emit('close-parking-charge-add-event', {search: true})
              } else {
                this.$message.error('保存失败:' + (resp.msg ? resp.msg : ''))
              }
            })
            .catch(error => {
              console.log(error)
              this.$message.error('保存失败')
            })
        } else {
          return false
        }
      })
    },
    findParkingArea () {
      this.parkingAreaSelectorVisible = true
    },
    handleParkingAreaSelectedEvent (selectedParkingArea) {
      this.parkingAreaSelectorVisible = false
      if (selectedParkingArea && 'parkingAreaId' in selectedParkingArea && this.parkingChargeForm.parkingAreaId !== selectedParkingArea['parkingAreaId']) {
        this.parkingChargeForm.parkingAreaId = selectedParkingArea['parkingAreaId']
        this.parkingChargeForm.parkingAreaNumber = selectedParkingArea['parkingAreaNumber']
      }
    },
    findRoom () {
      this.roomSelectorVisible = true
    },
    handleRoomSelectedEvent (selectedRoom) {
      this.roomSelectorVisible = false
      if (selectedRoom && 'roomId' in selectedRoom && this.parkingChargeForm.residentRoomId !== selectedRoom['roomId']) {
        this.parkingChargeForm.residentRoomId = selectedRoom['roomId']
        this.parkingChargeForm.residentRoomNumber = selectedRoom['roomNumber']
      }
    },
    onBack () {
      this.$emit('close-parking-charge-add-event')
    }
  }
}
</script>

<style lang="scss">
</style>